<?php

namespace App\Http\Controllers\Util;

use App\Notifications\RegisterNotify;
use Houdunwang\Aliyun\Aliyun;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class CodeController extends Controller
{
    public function send($account){
        dd($account);
        $code=code_random();
        if(filter_var($request->account,FILTER_VALIDATE_EMAIL)){
            $type='email';
            $user=User::firstOrNew(['email'=>$request->account]);
            $user->notify(New RegisterNotify($code));
        }elseif (preg_match('/^\d{11}$/',$request->account)){
            $type='mobile';

            $data = [
                // 短信签名
                'sign'=>'个人技术分享',
                // 短信模板
                'template'=>'SMS_144146118',
                // 手机号
                'mobile'=>$request->account,
                // 模板变量
                'vars'=>['code'=>$code]
            ];
            Aliyun::instance('Sms')->send($data);
        }else{
            return ['code'=>0,'message'=>'请输入正确的邮箱或手机号'];
        }
        return ['code'=>1,'message'=>'验证码发送成功'];
    }
}
